﻿package idv.etrex.display.object 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import idv.etrex.events.PlayerEvent;
	import idv.etrex.display.XGraphics;
	import idv.etrex.display.GraphicsBat;
		
		/** 
		 * 動畫撥放完時丟出
		 * @eventType Complete;
		 */
		[Event(name = "Complete", type = "idv.etrex.events.PlayerEvent")]
		
	/**
	 * 這是專門用來作動態遮罩的類別。
	 * @author etrex
	 */
	public class DynamicMask extends Sprite implements IPlayer
	{
		private var _dynamicStroke:DynamicStroke;
		private var _masked:DisplayObject;
		private var _onComplete:Function = function():void{};
		
		/**
		 * 用來控制每個影格要執行幾個Graphics指令
		 */
		public function get playingRate():uint {
			return _dynamicStroke.playingRate;
		}
		public function set playingRate(value:uint):void {
			_dynamicStroke.playingRate = value;
		}
		
		/**
		 * 是否正在播放
		 */
		public function get isPlaying():Boolean{
			return _dynamicStroke.isPlaying;
		}
		
		/**
		 * 存取 GraphicsBat
		 */
		public function get graphicsBat():GraphicsBat {
			return _dynamicStroke.graphicsBat;
		}
		public function set graphicsBat(value:GraphicsBat):void {
			_dynamicStroke.graphicsBat = graphicsBat;
		}
		
		/**
		 * 存取 XGraphics
		 */
		public function get xgraphics():XGraphics {
			return _dynamicStroke.xgraphics;
		}
		public function set xgraphics(value:XGraphics):void {
			_dynamicStroke.xgraphics = value;
		}

		/**
		 * 在丟出PlayerEvent.COMPLETE事件前會先執行
		 */
		public function set onComplete(value:Function):void {
			_onComplete = value;
		}
		
		/**
		 * 傳入被遮物件和XGraphics指令建立物件。
		 * 
		 * @param	masked 被遮物件
		 * @param	xg 動態遮罩
		 */
		public function DynamicMask(masked:DisplayObject = null, xg:XGraphics = null) 
		{
			//initial
			_dynamicStroke = new DynamicStroke(xg);	
			_masked = new Sprite();
			if (_masked != null)
			{
				this._masked = masked;
				
				//copy setting
				this.x = this._masked.x;
				this.y = this._masked.y;
				this.scaleX = this._masked.scaleX;
				this.scaleY = this._masked.scaleY;
				this.rotation = this._masked.rotation;
				
				this._masked.x = 0;
				this._masked.y = 0;
				this._masked.scaleX = 1;
				this._masked.scaleY = 1;
				this._masked.rotation = 0;
			}
			
			//mask setting
			_dynamicStroke.cacheAsBitmap = true;
			_masked.cacheAsBitmap = true;
			_masked.mask = _dynamicStroke;
			
			//event setting for _dynamicStroke
			_dynamicStroke.addEventListener(PlayerEvent.COMPLETE, dynamicStrokeComplete);
			
			this.addChild(_dynamicStroke);
			this.addChild(_masked);
		}
				
		/**
		 * 播放
		 */
		public function play():void  {
			_dynamicStroke.play();
		}
		
		/**
		 * 從頭播放
		 */
		public function replay():void  {
			_dynamicStroke.replay();
		}
		
		/**
		 * 暫停播放
		 */
		public function pause():void  {
			_dynamicStroke.pause();			
		}
		
		/**
		 * 停止播放
		 */
		public function stop():void  {
			_dynamicStroke.stop();			
		}
				
		private function dynamicStrokeComplete(e:PlayerEvent):void {
			_onComplete.apply(this);
			this.dispatchEvent(new PlayerEvent(PlayerEvent.COMPLETE));
		}
	}
}